var lon, lat, city, timerobj, timer;
mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		}
	}
});

function getChartOption(type,title) {
	var option = {}
	if (type != 'pie') {
		option = {
			title:{
				text:title,
			},
			tooltip: {
				trigger: 'item',
				show: false
			},
			toolbox: {
				show: true,
				feature: {
					changeLine: {
						show: true,
						title: 'line',
						icon:'image://../../images/charticon-line.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("line",title));
						}
					},
					changeBar: {
						show: true,
						title: 'line',
						icon:'image://../../images/charticon-bar.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("bar",title));
						}
					},
					changePie: {
						show: true,
						title: 'pie',
						icon:'image://../../images/charticon-pie.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("pie",title));
						}
					},
					dataView: {
						show: true,
						readOnly: false
					},
				}
			},
			grid: {
				x: 35,
				x2: 40,
				y: 50,
				y2: 50
			},
			legend: {
				data: ['故障', '火警']
			},
			xAxis: [{
				type: 'category',
				data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
					name: '故障',
					type: type,
					data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
					markLine: {
						data: [{
							type: 'average',
							name: '平均值'
						}]
					}
				},
				{
					name: '火警',
					type: type,
					data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
					markLine: {
						data: [{
							type: 'average',
							name: '平均值'
						}]
					}
				}
			]
		};
	} else {
		option = {
			title : {
				text: title,
			},
			tooltip: {
				trigger: 'item',
				show: false
			},
			toolbox: {
				show: true,
				feature: {
					changeLine: {
						show: true,
						title: 'line',
						icon:'image://../../images/charticon-line.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("line"));
						}
					},
					changeBar: {
						show: true,
						title: 'line',
						icon:'image://../../images/charticon-bar.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("bar"));
						}
					},
					changePie: {
						show: true,
						title: 'pie',
						icon:'image://../../images/charticon-pie.png',
						onclick: function() {
							var chart1 = echarts.init(document.getElementById('chart1'));
							chart1.setOption(getChartOption("pie"));
						}
					},
					dataView: {
						show: true,
						readOnly: false
					},
				}
			},
			series: [{
				name: '火警',
				type: 'pie',
				selectedMode: 'single',
                radius: [0, '40%'],				
				data: [
					  {value:335, name:'（火警）1月'},
                      {value:310, name:'（火警）2月'},
                      {value:234, name:'（火警）3月'},
                      {value:135, name:'（火警）4月'},
                      {value:1548, name:'（火警）5月'},
				      {value:335, name:'（火警）6月'},
				     {value:310, name:'（火警）7月'},
				     {value:234, name:'（火警）8月'},
				     {value:135, name:'（火警）9月'},
				     {value:1548, name:'（火警）10月'},
				     {value:135, name:'（火警）11月'},
				     {value:1548, name:'（火警）12月'}
				],
			},
			{
				name: '故障',
				type: 'pie',
                radius: ['80%', '60%'],
				data: [
					{value:335, name:'（故障）1月'},
					{value:310, name:'（故障）2月'},
					{value:234, name:'（故障）3月'},
					{value:135, name:'（故障）4月'},
					{value:1548, name:'（故障）5月'},
					{value:335, name:'（故障）6月'},
					{value:310, name:'（故障）7月'},
					{value:234, name:'（故障）8月'},
					{value:135, name:'（故障）9月'},
					{value:1548, name:'（故障）10月'},
					{value:135, name:'（故障）11月'},
					{value:1548, name:'（故障）12月'}
				],
			}
			]

		}
	}
	return option;
}


var chart1 = echarts.init(document.getElementById('chart1'));
chart1.setOption(getChartOption("bar","异常探测点"));

var chart2 = echarts.init(document.getElementById('chart2'));
chart2.setOption(getChartOption("bar","探测点故障率%"));

var chart3 = echarts.init(document.getElementById('chart3'));
chart3.setOption(getChartOption("bar","探测点重复故障率%"));

document.getElementById('selectBegin').addEventListener('tap', function() {
	var _self = this;
	_self.picker = new mui.DtPicker({
		type: "date"
	});
	_self.picker.show(function(re) {
		_self.innerHTML = re.text;
	});
})
document.getElementById('selectEnd').addEventListener('tap', function() {
	var _self = this;
	_self.picker = new mui.DtPicker({
		type: "date"
	});
	_self.picker.show(function(re) {
		_self.innerHTML = re.text;
	});
})


mui.plusReady(function() {
	storage.init();
	//注册登录事件
	appPage.registerCheckLoginEvent();

	initPage();

	//	if(!storageLocation.Lon || storageLocation.Lon == "") { //未拿到定位信息
	//		plus.nativeUI.showWaiting("定位中...");
	//		plus.geolocation.getCurrentPosition(function(position) {
	//			plus.nativeUI.closeWaiting();
	//			storageLocation.refreshData(position);
	//			storageLocation.log();
	//			initPage();				
	//		}, function(e) {
	//			plus.nativeUI.closeWaiting();
	//			mui.alert(JSON.stringify(e));
	//			//appUI.showTopTip("定位失败，请手动选择位置"+JSON.stringify(e));
	//		}, {
	//			geocode: true
	//		});
	//	} else {
	//		initPage();
	//	}

})

function initPage() {
	lon = storageLocation.Lon;
	lat = storageLocation.Lat;
	city = storageLocation.City;
	//document.getElementById("city").innerText = city;

	loadData();
}
//下拉刷新具体业务实现
function pulldownRefresh() {
	loadData();
}
//一次性拉取数据
function loadData() {
	request("/Index/getIndexLis", {
		lon: lon,
		lat: lat,
		cityid: storageLocation.CityId
	}, function(json) {
		if (json.code == "0") {

		} else {
			appUI.showTopTip(json.msg)
		}
		appPage.endPullRefresh();
	}, false, function() {
		appPage.endPullRefresh();
		var arr = document.getElementsByClassName("nodata");
		for (var i = 0; i < arr.length; i++) {
			arr[i].innerText = "暂无数据";
		}
	});
}
